/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
#include <QDebug>
#include <QFileDialog>
#include <QDir>
#include <QPixmap>
#include "testwidget.h"

TestWidget::TestWidget()
 : QWidget()
{
  ui.setupUi(this);

  connect(ui.btnConnect1, SIGNAL(clicked()),
          this, SLOT(reconnect1()));
  connect(ui.btnSelectImage, SIGNAL(clicked()),
          this, SLOT(selectImage()));
  connect(&mClient, SIGNAL(stateChanged()),
          this, SLOT(clientState()));
  connect(&mResource, SIGNAL(readyRead()),
          this, SLOT(updateImage()));
  connect(&mResource, SIGNAL(stateChanged()),
          this, SLOT(resStateChanged1()));

  mResource.init(&mClient,ui.url->text());

  clientState();
}

void TestWidget::reconnect1()
{
  mClient.close();
  QString host = ui.edtHost1->text();
  int port = ui.spnPort1->value();

  if (host.length() > 0 && port > 0) {
    mClient.connectToHost(host,port);
  }
}

void TestWidget::clientState()
{
  ui.chkReady1->setCheckState(mClient.isReady() ? Qt::Checked:Qt::Unchecked);
  ui.resourceWidget->setEnabled(mClient.isReady());
 
  if (!mClient.isReady())
    return;
 
  if (ui.chkPersistent->isChecked())
    mResource.open(Htr::Res::ReadWrite | Htr::Res::Create | Htr::Res::Persistent);
  else
    mResource.open(Htr::Res::ReadWrite | Htr::Res::Create);

  mResource.setMimeType(ui.mimeType->text());
}

void TestWidget::resStateChanged1()
{
  ui.boxState->setCurrentIndex(mResource.state());
}

void TestWidget::selectImage()
{
  static QDir dir;

  QString fname = QFileDialog::getOpenFileName(this,"Image", dir.absolutePath(), 
                                tr("Images (*.png *.xpm *.jpg *.bmp)"));

  if (fname.length() > 0) {
    dir = QDir(fname);
    QFile file(fname);
    if (!file.open(QIODevice::ReadOnly)) {
      qWarning() << "TestWidget::selectImage: cannot open" << fname;
      return;
    }

    QByteArray imageData = file.readAll();
    mResource.setMimeData(imageData); 
  }
}

void TestWidget::updateImage()
{
  QByteArray data = mResource.mimeData();
  ui.edtSize->setText(QString("%1").arg(data.size()));

  if (ui.chkUpdate->checkState() == Qt::Checked)
    return;

  QPixmap pxe;
  
  bool done = pxe.loadFromData(data);
  if (!done) {
    qWarning() << "TestWidget::updateImage: cannot load image";
  }

  pxe = pxe.scaled(ui.msgPixmap->width(), ui.msgPixmap->height());

  // Aus irgend einem Grund klappt hier (Qt4.6/64bit) 
  // das dynamische Aendern nicht.. daher ersetzen wir das QLabel einfach:
  delete ui.msgPixmap;
  ui.msgPixmap = new QLabel(ui.pixmapWidget);
  ui.pixmapWidget->layout()->addWidget(ui.msgPixmap);
  ui.msgPixmap->setPixmap(pxe);
}

